In [2]:
import opengm
import numpy
numObjects = 4
costs = numpy.random.rand(numObjects,numObjects)
print costs
In [4]:
gm = opengm.gm([numObjects]*numObjects)
unaryIds = gm.addFunctions(costs)
gm.addFactors(unaryIds,numpy.arange(numObjects))
f1To1=opengm.pottsFunction([numObjects,numObjects],10000.0, 0.0)
f1To1Id=gm.addFunction(f1To1)
for x0 in range(numObjects):
for x1 in range(x0+1,numObjects):
gm.addFactor(f1To1Id,[x0,x1])
In [8]:
Inf = opengm.inference.LazyFlipper
param = opengm.InfParam(maxSubgraphSize=1)
inf = Inf(gm=gm,parameter=param)
inf.infer()
arg = inf.arg()
print arg, gm.evaluate(arg)
print len(numpy.unique(arg))
In [9]:
Inf = opengm.inference.LazyFlipper
param = opengm.InfParam(maxSubgraphSize=2)
inf = Inf(gm=gm,parameter=param)
# use warm start
inf.setStartingPoint(arg)
inf.infer()
arg = inf.arg()
print arg, gm.evaluate(arg)
print len(numpy.unique(arg))
In [10]:
Inf = opengm.inference.LazyFlipper
param = opengm.InfParam(maxSubgraphSize=3)
inf = Inf(gm=gm,parameter=param)
inf.infer()
# use warm start
inf.setStartingPoint(arg)
arg = inf.arg()
print arg, gm.evaluate(arg)
print len(numpy.unique(arg))
In [11]:
gmB = opengm.gm([numObjects]*numObjects)
unaryIds = gmB.addFunctions(costs)
gmB.addFactors(unaryIds,numpy.arange(numObjects))
Out[11]:
In [12]:
cplexParam = opengm.InfParam(integerConstraint=True)
lpCplex = opengm.inference.LpCplex(gm=gm,parameter=cplexParam)
for x0 in range(numObjects):
for x1 in range(x0+1,numObjects):
for label in range(numObjects):
lpVarx0 = lpCplex.lpNodeVariableIndex(x0,label)
lpVarx1 = lpCplex.lpNodeVariableIndex(x1,label)
constraintVars = [lpVarx0,lpVarx1]
constraintCoeff = [1.0,1.0]
lowerBound = 0.0
upperBound = 1.0
lpCplex.addConstraint(constraintVars,constraintCoeff,lowerBound,upperBound)
lpCplex.infer()
arg = lpCplex.arg()
print arg, gm.evaluate(arg)
print len(numpy.unique(arg))